//
//  WmcDevNetworkApiListVC.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/22.
//

import UIKit

// MARK: - 扩展WmcDevNetworkApiListVC + WmcVMProtocol

extension WmcDevNetworkApiListVC: WmcVMProtocol {
    typealias VMType = WmcDevNetworkApiListVM
}

// MARK: - [WmcDevNetworkApiListVC]

class WmcDevNetworkApiListVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "接口列表"
    }

    // Internal

    override func makeUI() {
        makeNavView(showShadow: false)

        addSubview(tableView)

        tableView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom).offset(10)
            make.left.right.bottom.equalToSuperview()
        }
    }

    // Private

    private lazy var tableView: UITableView = {
        let view = UITableView(frame: CGRect.zero, style: .plain)
        view.backgroundColor = .wmc_white_FFFFFF
        view.delegate = self
        view.dataSource = self
        view.register(WmcDevNetworkApiCell.self, forCellReuseIdentifier: "WmcDevNetworkApiCell")
        view.separatorStyle = .none
        return view
    }()
}

// MARK: - UITableViewDelegate, UITableViewDataSource

extension WmcDevNetworkApiListVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in _: UITableView) -> Int {
        return viewModel.dataSource.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let group = viewModel.dataSource[section]
        return group.list.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcDevNetworkApiCell = tableView.dequeueReusableCell(withIdentifier: "WmcDevNetworkApiCell", for: indexPath) as! WmcDevNetworkApiCell
        let model = viewModel.dataSource[indexPath.section].list[indexPath.row]
        cell.bindModel(model)
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = viewModel.dataSource[indexPath.section].list[indexPath.row]
//        WmcNavigationManager.push(.devApiDetail, parms: model, animated: true)
    }

    func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
        return 110
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view: UIView = .init()
        view.backgroundColor = .wmc_grayH22_FFF5E7

        let group = viewModel.dataSource[section]

        let label = UILabel()
        label.text = group.name
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        view.addSubview(label)

        label.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.right.top.bottom.equalToSuperview()
        }

        return view
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30
    }
}
